package com.itheima.tanhua.server.service;

import cn.hutool.jwt.JWTUtil;
import com.itheima.tanhua.common.utils.JwtUtils;
import com.itheima.tanhua.domain.Question;
import com.itheima.tanhua.domain.Settings;
import com.itheima.tanhua.domain.User;
import com.itheima.tanhua.domain.UserInfo;
import com.itheima.tanhua.dubbo.api.UserApi;
import com.itheima.tanhua.po.LoginReginfoPo;
import com.itheima.tanhua.po.NotificationsPo;
import com.itheima.tanhua.server.holder.TokenHolder;
import com.itheima.tanhua.vo.LoginVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author itheima
 * @since 2022-01-13
 */
@Service
public class UserService {

    @DubboReference
    private UserApi userApi;

    @Autowired
    private RedisTemplate redisTemplate;

    public void sendMessage(String phone) {
        // 生成验证码
        String code = "1234";

        // 发送验证码


        // 存储redis
        redisTemplate.opsForValue().set("verifyCode_" + phone, code);
    }

    public LoginVo loginVerification(String phone, String verificationCode) throws Exception {

        String cacheKey = "verifyCode_" + phone;

        boolean isNew = true;

        String code = (String) redisTemplate.opsForValue().get(cacheKey);
        if (!verificationCode.equals(code)) {
            throw new Exception("验证码错误");
        }

        // 查询数据库，看看用户在不在
        User user = userApi.findByPhone(phone);
        if (Objects.isNull(user)) {
            isNew = false;
            user = userApi.createOne(phone);
        }

        Map<String, Object> tokenData = new HashMap<>();
        tokenData.put("phone", phone);
        tokenData.put("id", user.getId());

        String token = JwtUtils.getToken(tokenData);

        LoginVo vo = new LoginVo();
        vo.setToken(token);
        vo.setIsNew(isNew);

        return vo;
    }

    public void loginReginfo(LoginReginfoPo po) {

        // 添加UserInfo表记录

        String id = TokenHolder.get();

        UserInfo userInfo = new UserInfo();

        userInfo.setId(id);
        userInfo.setNickname(po.getNickname());
        userInfo.setBirthday(po.getBirthday());
        userInfo.setCity(po.getCity());
        userInfo.setGender(po.getGender());
        userInfo.setCoverPic(po.getHeader());

        userApi.addUserInfo(userInfo);
    }

    public void loginReginfoHead(MultipartFile headPhoto) {

        // 上传图片

        // 获得上传路径
        String pic = "http://www.it.com";

        String id = TokenHolder.get();

        UserInfo userInfo = userApi.findUserInfoByUserId(id);
        if (Objects.isNull(userInfo)) {
            return;
        }

        // 保存到UserInfo信息中
        userInfo.setCoverPic(pic);
        userApi.updateUserInfo(userInfo);
    }

    public UserInfo getMyUserInfo() {
        String id = TokenHolder.get();
        return userApi.findUserInfoByUserId(id);
    }

    public Boolean updateUserInfo(UserInfo userInfo) {
        String id = TokenHolder.get();
        userInfo.setId(id);
        return userApi.updateUserInfo(userInfo);
    }


    public Boolean updateNotifications(NotificationsPo notificationsPo) {
        String id = TokenHolder.get();

        Settings settings = new Settings();
        settings.setUserId(id);
        BeanUtils.copyProperties(notificationsPo, settings);
        return userApi.updateSettingByUserId(settings);

    }

    public Settings getSettings() {
        String id = TokenHolder.get();
        return userApi.findSettingsByUserId(id);
    }

    public Boolean updateQuestion(String content) {
        String id = TokenHolder.get();
        Question question = userApi.findQuestionByUserId(id);
        question.setTxt(content);
        question.setUserId(id);
        if (Objects.isNull(question)) {
            return userApi.createQuestion(question);
        }

        return userApi.updateQuestionByUserId(question);
    }
}
